home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- *
- * Copyright © Palm Computing 1998 -- All Rights Reserved
- *
- *
- **********************************************************************/
-
- #include <Pilot.h> // all the system toolbox headers
- #include "GrayRsc.h" // application resource defines
- #include "GrayEdit.h"
-
- #define gBigBW 0
- #define gSmallBW 1
- #define gBigGray 2
- #define gSmallGray 3
-
- // external globals we use
- extern Boolean gSupportsGrayAPIs;
- extern Boolean gHasGrayScreen;
-
- // local globals
- BitmapPtr gBmps[4];
- int gCurrentlyEditing;
- int gCurrentDepth;
- int gPenColor;
-
- typedef Byte * UBP;
-
- /***********************************************************************/
- static void DrawARect(int x, int y, int wid, int hei, int color)
- {
- static const RGBColorType black = {0x00, 0x00, 0x00, 0x00};
- static const RGBColorType dkGray = {0x00, 0x55, 0x55, 0x55};
- static const RGBColorType ltGray = {0x00, 0xAA, 0xAA, 0xAA};
- static const RGBColorType white = {0x00, 0xFF, 0xFF, 0xFF};
- static CustomPatternType dkGrayPat = {0xAAAA, 0x5555, 0xAAAA, 0x5555};
- static CustomPatternType ltGrayPat = {0x8822, 0x8822, 0x8822, 0x8822};
-
- RGBColorType theColor, oldcolor;
- RectangleType r;
- CustomPatternType oldpattern, ppat;
- FormPtr frm;
- int i;
-
- r.topLeft.x = x;
- r.topLeft.y = y;
- r.extent.x = wid;
- r.extent.y = hei;
-
- if (gSupportsGrayAPIs) {
- switch (color) {
- case 0: WinSetForeColor(&white, &oldcolor); break;
- case 1: WinSetForeColor(<Gray, &oldcolor); break;
- case 2: WinSetForeColor(&dkGray, &oldcolor); break;
- default: WinSetForeColor(&black, &oldcolor); break;
- }
- //WinSetForeColor(&theColor, &oldcolor);
- //WinSetPattern(blackPattern);
- //WinDrawRectangle(&r, 0);
- for (i=0; i<hei; i++) {
- WinDrawLine(r.topLeft.x, r.topLeft.y + i, r.topLeft.x + wid, r.topLeft.y + i);
- }
- WinSetForeColor(&oldcolor, &oldcolor);
- WinSetForeColor(&black, &oldcolor); // this shouldn't have to be done but it is!
- } else {
- if (color==3)
- WinDrawRectangle(&r, 0);
- else if (color==0)
- WinEraseRectangle(&r, 0);
- else {
- WinGetPattern(oldpattern);
- if (color==2)
- WinSetPattern(dkGrayPat);
- else
- WinSetPattern(ltGrayPat);
- WinFillRectangle(&r, 0);
- WinSetPattern(blackPattern); // this should be oldPattern, I think, but that doesn't restore things right.
- }
- }
- }
-
- /***********************************************************************/
- static void DrawPenPaint(int which, int color)
- {
- RectangleType r;
- FormPtr frm;
-
- frm = FrmGetActiveForm();
- FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, which), &r);
-
- WinEraseRectangle(&r, 0);
- DrawARect(r.topLeft.x+1, r.topLeft.y+1, r.extent.x -3, r.extent.y -2, color);
-
- //RctInsetRectangle(&r, -1);
- if (color == gPenColor) {
- WinDrawRectangleFrame(2, &r);
- } else {
- WinEraseRectangleFrame(2, &r);
- WinDrawRectangleFrame(simpleFrame, &r);
- }
- }
-
- /***********************************************************************/
- void DrawPenPaints()
- {
- DrawPenPaint(GrayMainPen0Gadget,0);
- DrawPenPaint(GrayMainPen1Gadget,1);
- DrawPenPaint(GrayMainPen2Gadget,2);
- DrawPenPaint(GrayMainPen3Gadget,3);
-
- }
-
- /***********************************************************************/
- void DrawEditor()
- {
- RectangleType r;
- FormPtr frm;
-
- frm = FrmGetActiveForm();
- FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, GrayMainPixelGadget), &r);
- WinEraseRectangle(&r, 0);
- RctInsetRectangle(&r, -1);
- WinDrawRectangleFrame(simpleFrame, &r);
- }
-
-
- /***********************************************************************/
- static void DrawBitmapInGadget(int whichOne, Word obj)
- {
- RectangleType r;
- FormPtr frm;
- BitmapPtr pBmp;
-
- frm = FrmGetActiveForm();
- FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, obj), &r);
- RctInsetRectangle(&r, -1);
- WinDrawRectangleFrame(simpleFrame, &r);
-
- pBmp = gBmps[whichOne];
- if (pBmp)
- WinDrawBitmap(pBmp, r.topLeft.x, r.topLeft.y);
- else {
- RctInsetRectangle(&r, 1);
- WinEraseRectangle(&r, 0);
- }
- }
-
-
- /***********************************************************************/
- void DrawEditorAndDisplays()
- {
- DrawEditor();
- DrawPenPaints();
- DrawBitmapInGadget(gBigBW, GrayMainBigBWGadget);
- DrawBitmapInGadget(gSmallBW, GrayMainSmallBWGadget);
- DrawBitmapInGadget(gBigGray, GrayMainBigGrayGadget);
- DrawBitmapInGadget(gSmallGray, GrayMainSmallGrayGadget);
- }
-
- /***********************************************************************/
- static UBP FindBitsPtr(BitmapPtr bmp, int whichDepth)
- // if whichDepth is -1 it'll just use the first depth
- {
- Word nextOffset;
-
- if (bmp->pixelSize) {
- do {
- // thisDepth = (UBP) &(bmp->pixelSize);
- //nextOffset = *((Word *) (thisDepth+2)); // hack to read a word out
- if ( (bmp->pixelSize==whichDepth) || (whichDepth <0))
- return sizeof(BitmapType) + ((UBP) bmp);
-
- nextOffset = bmp->nextDepthOffset;
- bmp = (BitmapPtr) (nextOffset + ((UBP) bmp));
- } while (nextOffset);
- return 0; // we've reached the end of the structure and haven't found it; bail.
- } else {
- // old-style means it is a one-bit image
- return (UBP) (++bmp);
- }
- }
-
-
- /***********************************************************************/
- static int calcRowBytes(int w, int depth)
- {
- int rowBytes;
- int divisor;
-
- divisor = 8;
- while (depth > 1) {
- divisor = divisor >> 1;
- depth--;
- }
- rowBytes = 1 + (w >> depth); // / divisor
- if (rowBytes & 0x01)
- rowBytes ++; // it must be even so bump up
-
- return rowBytes;
- }
-
- /***********************************************************************/
- static void SetPixel(Byte value, SWord x, SWord y, BitmapPtr bmp)
- // always works with the first depth in the image
- // supports up to 8 bits deep
- {
- int col, depth, shift, mask, theByte, remainder;
- UBP bits;
- int byteOffset;
-
- // silently ignore bad param
- if ((x > bmp->width) || (y > bmp->height))
- return;
-
- bits = FindBitsPtr(bmp, -1);
- if (bmp->pixelSize)
- depth = bmp->pixelSize;
- else
- depth=1;
-
- switch (depth) {
- case 0:
- case 1: shift=3; break;
- case 2: shift=2; break;
- case 4: shift=1; break;
- case 8: shift=0; break;
- }
-
- byteOffset = (bmp->rowBytes * y) + (x >> shift);
- remainder = x - ((x >> shift) << shift);
-
- if (depth==1) {
- value = value << 7; // put it in the highest position
- mask = 0x80;
- while (remainder) {
- mask = mask >> 1;
- value=value >> 1;
- remainder--;
- }
- mask = ~mask;
- } else if (depth==2) {
- value = value << 6;
- mask = 0xc0;
- while (remainder) {
- value = value >> 2;
- mask = mask >> 2;
- remainder--;
- }
- mask = ~mask;
- } else if (depth==8) {
- mask=0xff;
- }
-
- bits[byteOffset] = value | (bits[byteOffset] & (mask));
- }
-
- /***********************************************************************/
- static int GetPixel(SWord x, SWord y, BitmapPtr bmp)
- {
- return 0;
- }
-
-
- /***********************************************************************/
- static void DrawBigPixel(int x, int y, int color)
- {
- /* static const RGBColorType black = {0x00, 0x00, 0x00, 0x00};
- static const RGBColorType dkGray = {0x00, 0x55, 0x55, 0x55};
- static const RGBColorType ltGray = {0x00, 0xAA, 0xAA, 0xAA};
- static const RGBColorType white = {0x00, 0xFF, 0xFF, 0xFF};
- static CustomPatternType dkGrayPat = {0xAAAA, 0x5555, 0xAAAA, 0x5555};
- static CustomPatternType ltGrayPat = {0x8822, 0x8822, 0x8822, 0x8822};
-
- RGBColorType theColor, oldcolor;
- CustomPatternType oldpattern, ppat;
- */ FormPtr frm;
- RectangleType r;
- int i;
-
- frm = FrmGetActiveForm();
- FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, GrayMainPixelGadget), &r);
-
- DrawARect((x*4) + r.topLeft.x, (4*y) + r.topLeft.y, 4,4, color);
-
-
- /* RctOffsetRectangle(&r, x<<2, y<<2);
- r.extent.x = 4;
- r.extent.y = 4;
-
- if (gSupportsGrayAPIs) {
- switch (color) {
- case 1: theColor = dkGray; break;
- case 2: theColor = ltGray; break;
- case 3: theColor = white; break;
- default: theColor = black; break;
- }
- WinSetForeColor(&theColor, &oldcolor);
- //WinSetPattern(blackPattern);
- //WinDrawRectangle(&r, 0);
- for (i=0; i<r.extent.
- WinSetForeColor(&oldcolor, &oldcolor);
- } else {
- if (color==3)
- WinDrawRectangle(&r, 0);
- else if (color==0)
- WinEraseRectangle(&r, 0);
- else {
- WinGetPattern(oldpattern);
- if (color==2)
- WinSetPattern(dkGrayPat);
- else
- WinSetPattern(ltGrayPat);
- WinFillRectangle(&r, 0);
- WinSetPattern(blackPattern); // this should be oldPattern, I think, but that doesn't restore things right.
- }
- }
- */
- }
-
- /***********************************************************************/
- void HitPixel(SWord x, SWord y)
- {
- FormPtr frm;
- RectangleType r;
- BitmapPtr bmp;
-
- frm = FrmGetActiveForm();
- FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, GrayMainPixelGadget), &r);
-
- // convert from screen pixels to the appropriate fatbit pixel
- x = (x-r.topLeft.x) >> 2;
- y = (y-r.topLeft.y) >> 2;
-
- bmp = gBmps[gCurrentlyEditing];
-
- // silently ignore bad click location
- if ((x > bmp->width) || (y > bmp->height))
- return;
-
- SetPixel(gPenColor, x, y, bmp);
-
- DrawBigPixel(x, y, gPenColor);
-
- //inefficient...
- DrawBitmapInGadget(gBigBW, GrayMainBigBWGadget);
- DrawBitmapInGadget(gSmallBW, GrayMainSmallBWGadget);
- DrawBitmapInGadget(gBigGray, GrayMainBigGrayGadget);
- DrawBitmapInGadget(gSmallGray, GrayMainSmallGrayGadget);
- }
-
-
- /***********************************************************************/
- void SaveAndSwitch(Word switchTo)
- {
- FormPtr frm;
- int newOne;
-
- switch (switchTo) {
- case GrayMainBigBWGadget: newOne = gBigBW; break;
- case GrayMainSmallBWGadget: newOne = gSmallBW; break;
- case GrayMainBigGrayGadget: newOne = gBigGray; break;
- case GrayMainSmallGrayGadget: newOne = gSmallGray; break;
- }
-
- if (gCurrentlyEditing != newOne)
- gCurrentlyEditing=newOne;
- if ((newOne==gBigBW) || (newOne==gSmallBW))
- gCurrentDepth=1;
- else
- gCurrentDepth=2;
-
- DrawEditorAndDisplays();
- }
-
- /***********************************************************************/
- static BitmapPtr AllocateBmp(int w, int h, int depth)
- {
- unsigned short rowBytes; // must be even.
- BitmapPtr bmp;
- int bmpSize;
-
- rowBytes = calcRowBytes(w, depth);
-
- bmpSize = sizeof(BitmapType) + sizeof(ColorTableType) + (rowBytes * h * depth);
- bmp = (BitmapPtr) MemPtrNew(bmpSize);
- MemSet(bmp, bmpSize, 0); // guarantee everything is cleared out
-
- if (bmp) {
- bmp->width = w;
- bmp->height = h;
- bmp->rowBytes = rowBytes;
- if (depth>1) {
- // make a 3.0-style bitmap
- bmp->pixelSize = depth;
- bmp->version = 1;
- bmp->nextDepthOffset = 0; // this only has a single image in it
-
- } else {
- // make an old-style bitmap object
- // ... everything must be set to 0, which is done above.
- }
- }
-
- return bmp;
- }
-
- /***********************************************************************/
- void SetPenTo(int value)
- {
- gPenColor=value;
- DrawPenPaints();
- }
-
- /***********************************************************************/
- void InitGrayEdit()
- {
- gCurrentlyEditing=gBigBW;
- gPenColor=3; // in B&W setPixel will do ok with this
- gBmps[gBigBW] = AllocateBmp(29,29,1); // 29x29 one bit image
- gBmps[gSmallBW] = AllocateBmp(15,9, 1);
- gBmps[gBigGray] = AllocateBmp(29,29,2);
- gBmps[gSmallGray] = AllocateBmp(15,9, 2);
- }
-